package com.xyh.reward_kotlin.api

import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

abstract class ApiCallback<T> : Callback<T> {

    override fun onResponse(call: Call<T>, response: Response<T>) {
        if (response.isSuccessful && response.body() != null) {
            onSuccess(response.body()!!)
        } else {
            onError("请求失败，状态码：${response.code()}")
        }
    }

    override fun onFailure(call: Call<T>, t: Throwable) {
        onError("请求失败：${t.message}")
    }

    // 由调用方实现
    abstract fun onSuccess(result: T)
    abstract fun onError(errorMsg: String?)
}
